package a1.Sample1;

import java.io.IOException;

import edu.cmu.arch.pipesandfilters.frame.Frame;
import edu.cmu.arch.pipesandfilters.frame.Variable;


/**
 * A first past at working out the RejectFilter. 
 *    - need to implement GetExtrapolatedValue
 *    - need to make configurable?
 *    - may need to write out to file
 * @author Steven Thomas (steventh@andrew.cmu.edu)
 */

public class RejectFilter extends FrameFilter {
	public void run()
	{
		System.out.print("\n" + this.getName() + "::RejectFilter Reading");
		Frame frame = null;
		Number LastValidMeasurement;

		try{
			while(true) {
				frame = this.readFrame();

				// Identify wild points on Pressure
				Double Pressure = frame.getValue(Variable.Pressure).doubleValue();
				// if pressure is invalid

				if((Pressure > 80)||(Pressure < 50))
				{
					System.out.println("invalid pressure");

					Double NewPressure = GetExtrapolatedPressure();
					frame.putValue(Variable.Pressure,NewPressure);
					frame.putValue(Variable.ExtrapolatedId,Variable.Pressure.ordinal());

				}else
				{
					// measurement is valid so save it for later
					LastValidMeasurement = Pressure;
				}

			}//while
		} catch (IOException ioe) {
			System.out.println("IOException while reading input stream:  " + ioe.getMessage());		  
		}catch (EndOfStreamException e) {
			System.out.println("End of stream caught");
		} finally {
			ClosePorts();
			System.out.print("\n" + this.getName() + "::Middle Exiting");
		}

	}// run

	private double GetExtrapolatedPressure()
	{
		//todo implement function
		return 60;
	}
	
}// RejectFilter